﻿using UnityEngine;
using System.Collections;
using NetMsg;
using ProtoBuf;
using System.IO;

public class test : MonoBehaviour {
    private string szUrl = "http://127.0.0.1:8003";
   // private string szUrl = "http://121.40.147.116:8006";  //knightmare
   // private string szUrl = "http://121.40.147.116:8007";  //parkour
    private UserInfo m_User_info = new UserInfo();

    void F(ref A8_CMD a)
    {
        a.user_id = m_User_info.user_id;
        a.key = System.Text.Encoding.Default.GetBytes(SystemInfo.deviceUniqueIdentifier);

      //  a.user_id = 65553;
     //   a.key = System.Text.Encoding.Default.GetBytes("7173eeebe7bd7d8dca02cf9637cb386ae4037723");
   }

    byte[] O2Bytes<T>(ref T t)
    {
        Stream parameters = new MemoryStream();
        Serializer.Serialize<T>(parameters, t);
        parameters.Position = 0;
        int length = (int)parameters.Length;
        byte[] o = new byte[length];
        parameters.Read(o, 0, length);
        return o;
    }
    byte[] S_CMD<P>(ref A8_CMD a, uint cmd_id, ref P p)
    {
        F(ref a);
        CMD c = new CMD();
        c.cmd_id = cmd_id;
        if (p != null)
        {
            c.cmd_parameter = O2Bytes<P>(ref p);
        }
        a.cmd_list.Add(c);
        return O2Bytes<A8_CMD>(ref a);
    }
    byte[] S_CMD(ref A8_CMD a, uint cmd_id)
    {
        F(ref a);
        CMD c = new CMD();
        c.cmd_id = cmd_id;
        a.cmd_list.Add(c);
        return O2Bytes<A8_CMD>(ref a);
    }
    T DT<T>(byte[] o)
    {
        Stream s = new MemoryStream(o);
        return Serializer.Deserialize<T>(s);
    }
    uint CheckResult<T>(byte[] bytes, uint cmd_id, ref T t)
    {
        A8_CMD rec_cmd = DT<A8_CMD>(bytes);

        int cmd_size = rec_cmd.cmd_list.Count;
        for (int i = 0; i < cmd_size; ++i)
        {
            CMD rec_c = rec_cmd.cmd_list[i];
            if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
            {
                if (rec_c.cmd_id == cmd_id)
                {
                    t = DT<T>(rec_c.cmd_parameter);
                    return rec_c.cmd_result;
                }
            }
            else
            {
                return rec_c.cmd_result;
            }
        }
        return (uint)Command_Result.cmd_user_not_found;
    }

    uint CheckResult(byte[] bytes, uint cmd_id, ref byte[] para)
    {
        A8_CMD rec_cmd = DT<A8_CMD>(bytes);

        int cmd_size = rec_cmd.cmd_list.Count;
        for (int i = 0; i < cmd_size; ++i)
        {
            CMD rec_c = rec_cmd.cmd_list[i];
            if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
            {
                if (rec_c.cmd_id == cmd_id)
                {
                    para = rec_c.cmd_parameter;
                    return rec_c.cmd_result;
                }
            }
            else
            {
                return rec_c.cmd_result;
            }
        }
        return (uint)Command_Result.cmd_user_not_found;
    }
	// Use this for initialization
	void Start () {
   //     m_User_info.user_id = 65536;
        StartCoroutine(Reg());
        //StartCoroutine(Purchase());
        //StartCoroutine(mail((uint)Mail_Type.mail_friend_req + (uint)Mail_Type.mail_comm));
        //StartCoroutine(GetRank(1, 1, 100, 1));
        //uint n = get_liner_hash("毛泽东", 0);
        //Debug.Log("毛泽东:" + n);
 /*       Debug.Log("Tutorial_2-1-1:" + get_liner_hash("Tutorial_2-1-1", 0));
        Debug.Log("Tutorial_2-1-2:" + get_liner_hash("Tutorial_2-1-2", 0));
        Debug.Log("Tutorial_2-1-3:" + get_liner_hash("Tutorial_2-1-3", 0));
        Debug.Log("Tutorial_2-1-4:" + get_liner_hash("Tutorial_2-1-4", 0));

        Debug.Log("1Tutorial_2-1-1:" + get_liner_hash1("Tutorial_2-1-1", 0));
        Debug.Log("1Tutorial_2-1-2:" + get_liner_hash1("Tutorial_2-1-2", 0));
        Debug.Log("1Tutorial_2-1-3:" + get_liner_hash1("Tutorial_2-1-3", 0));
        Debug.Log("1Tutorial_2-1-4:" + get_liner_hash1("Tutorial_2-1-4", 0));

        Debug.Log("Tutorial_1-1-1:" + get_liner_hash("Tutorial_1-1-1", 0));
        Debug.Log("Tutorial_1-1-2:" + get_liner_hash("Tutorial_1-1-2", 0));
        Debug.Log("Tutorial_1-1-3:" + get_liner_hash("Tutorial_1-1-3", 0));
        Debug.Log("Tutorial_1-1-4:" + get_liner_hash("Tutorial_1-1-4", 0));

        Debug.Log("Tutorial_3-1-1:" + get_liner_hash("Tutorial_3-1-1", 0));
        Debug.Log("Tutorial_3-1-2:" + get_liner_hash("Tutorial_3-1-2", 0));
        Debug.Log("Tutorial_3-1-3:" + get_liner_hash("Tutorial_3-1-3", 0));
        Debug.Log("Tutorial_3-1-4:" + get_liner_hash("Tutorial_3-1-4", 0));*/
	}
    IEnumerator Purchase(uint item_id, uint item_count)
    {
        A8_CMD a8 = new A8_CMD();
        Purchase_r_Parameters param = new Purchase_r_Parameters();
        param.item_id = item_id;
        param.item_count = item_count;
        byte[] buffer = S_CMD<Purchase_r_Parameters>(ref a8, (uint)Command_ID.cmd_purchase_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_purchase_a)
                    {
                        Purchase_a_Parameters a = DT<Purchase_a_Parameters>(rec_c.cmd_parameter);
                        Debug.Log("Purchase: " + a.item_id + ":" + a.ToString());
                        StartCoroutine(BuyGoods(1, 1));
                    }
                }
                else
                {
                    Debug.Log("Purchase Error: " + rec_c.cmd_result);
                }
            }
        }
    }

    IEnumerator mail(uint mail_type)
    {
        A8_CMD a8 = new A8_CMD();
        MailList_r_Parameters param = new MailList_r_Parameters();
        param.req_mail_type = mail_type;
        byte[] buffer = S_CMD<MailList_r_Parameters>(ref a8, (uint)Command_ID.cmd_maillist_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_maillist_a)
                {
                    MailList_a_Parameters rega = DT<MailList_a_Parameters>(rec_c.cmd_parameter);
                    Debug.Log(rega.mail_list.Count);
                    for (int j = 0; j < rega.mail_list.Count; ++j)
                    {
                        Mail m = rega.mail_list[j];
                        if (m.attachment.Length > 0)
                        {
                            Debug.Log(m.mail_id + "attachment is not NULL");
                            Debug.Log(System.Text.Encoding.Default.GetString(m.attachment));
                        }
                        else
                        {
                            Debug.Log(m.mail_id + "attachment is NULL");
                        }
                        if (m.mail_flag == 0)
                        {
                         //   StartCoroutine(ReadMail(m.mail_id));
                        }
                        if (m.mail_type == (uint)Mail_Type.mail_friend_req)
                        {
                            Debug.Log("Friend Req:" + System.Text.Encoding.UTF8.GetString(m.sender_name));
                        }
                        else if (m.mail_type == (uint)Mail_Type.mail_comm)
                        {
                            Debug.Log("Comm mail:" + System.Text.Encoding.UTF8.GetString(m.sender_name));
                         }
                        Debug.Log("mailType check: r " + mail_type + " ,a " + m.mail_type);
                    }
                }
            }
        }
    }
    IEnumerator Reg()
    {
        DeviceInfo device = new DeviceInfo();
        device.Unique_id = System.Text.Encoding.Default.GetBytes(SystemInfo.deviceUniqueIdentifier);
 //     device.Unique_id = System.Text.Encoding.Default.GetBytes("21d76448be251396a7f60c0db2c31dde10b5f897");
        A8_CMD a8 = new A8_CMD();
        a8.key = device.Unique_id;
        CMD cmd = new CMD();
        cmd.cmd_id = (uint)Command_ID.cmd_reg_r;
        Reg_r_Parameters reg = new Reg_r_Parameters();
        reg.dev_info = device;

        Stream parameters = new MemoryStream();
        Serializer.Serialize<Reg_r_Parameters>(parameters, reg);

        parameters.Position = 0;
        int length1 = (int)parameters.Length;
        byte[] buffer1 = new byte[length1];
        parameters.Read(buffer1, 0, length1);

        cmd.cmd_parameter = buffer1;
        a8.cmd_list.Add(cmd);

        Stream streamA8 = new MemoryStream();
        Serializer.Serialize<A8_CMD>(streamA8, a8);

        streamA8.Position = 0;
        int length = (int)streamA8.Length;
        byte[] buffer = new byte[length];
        streamA8.Read(buffer, 0, length);


        WWW getData = new WWW(szUrl, buffer);
        
        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            Stream stream_rec = new MemoryStream(getData.bytes);

            A8_CMD rec_cmd = Serializer.Deserialize<A8_CMD>(stream_rec);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == 2)
                {
                    System.IO.Stream oo;
                    byte[] buff = rec_c.cmd_parameter;
                    oo = new MemoryStream(buff);
                    Reg_a_Parameters rega = Serializer.Deserialize<Reg_a_Parameters>(oo);
                    if (rega.user_info != null)
                    {
                        if (rega.user_info.nick_name != null)
                        {
                            Debug.Log("NULL set");
                        }
                        else
                        {
                            Debug.Log("nick name is null");
                        }
                    }
                    Debug.Log("》" + rega.user_id);
                    Debug.Log("》" + rega.user_info.user_id);
                    StartCoroutine(Login());
                }
            }
        }
    }
    IEnumerator ReadMail(ulong id)
    {
        A8_CMD a8 = new A8_CMD();
        a8.key = System.Text.Encoding.Default.GetBytes(SystemInfo.deviceUniqueIdentifier);
        a8.user_id = m_User_info.user_id;

        a8.user_id = 65536;
        a8.key = System.Text.Encoding.Default.GetBytes("140b2fea09ee06a24824f14427b7f8aecda4e642");

        CMD cmd = new CMD();
        cmd.cmd_id = (uint)Command_ID.cmd_mail_read_r;

        MailRead_r_Parameters mrp = new MailRead_r_Parameters();
        mrp.mail_id = id;

        Stream parameters = new MemoryStream();
        Serializer.Serialize<MailRead_r_Parameters>(parameters, mrp);

        parameters.Position = 0;
        int length1 = (int)parameters.Length;
        byte[] buffer1 = new byte[length1];
        parameters.Read(buffer1, 0, length1);

        cmd.cmd_parameter = buffer1;

        a8.cmd_list.Add(cmd);

        Stream streamA8 = new MemoryStream();
        Serializer.Serialize<A8_CMD>(streamA8, a8);

        streamA8.Position = 0;
        int length = (int)streamA8.Length;
        byte[] buffer = new byte[length];
        streamA8.Read(buffer, 0, length);

        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            Stream stream_rec = new MemoryStream(getData.bytes);

            A8_CMD rec_cmd = Serializer.Deserialize<A8_CMD>(stream_rec);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_mail_read_a)
                {
                    if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                    {
                        System.IO.Stream oo;
                        byte[] buff = rec_c.cmd_parameter;
                        oo = new MemoryStream(buff);
                        MailRead_a_Parameters m_a = Serializer.Deserialize<MailRead_a_Parameters>(oo);
                        Debug.Log("Mail Read:" + m_a.mail_id);
                    }
                    else
                    {
                        Debug.Log("Mail Read Error: " + rec_c.cmd_result);
                    }
                }
            }
        }
    }

    IEnumerator Login()
    {
        A8_CMD a8 = new A8_CMD();
        a8.key = System.Text.Encoding.Default.GetBytes(SystemInfo.deviceUniqueIdentifier);
        CMD cmd = new CMD();
        cmd.cmd_id = (uint)Command_ID.cmd_login_r;

        Login_r_Parameters lgi = new Login_r_Parameters();
        lgi.unique_id = a8.key;

        Stream parameters = new MemoryStream();
        Serializer.Serialize<Login_r_Parameters>(parameters, lgi);

        parameters.Position = 0;
        int length1 = (int)parameters.Length;
        byte[] buffer1 = new byte[length1];
        parameters.Read(buffer1, 0, length1);

        cmd.cmd_parameter = buffer1;
 
        a8.cmd_list.Add(cmd);
 
        Stream streamA8 = new MemoryStream();
        Serializer.Serialize<A8_CMD>(streamA8, a8);

        streamA8.Position = 0;
        int length = (int)streamA8.Length;
        byte[] buffer = new byte[length];
        streamA8.Read(buffer, 0, length);

        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            Stream stream_rec = new MemoryStream(getData.bytes);

            A8_CMD rec_cmd = Serializer.Deserialize<A8_CMD>(stream_rec);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_login_a)
                {
                    if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                    {
                        System.IO.Stream oo;
                        byte[] buff = rec_c.cmd_parameter;
                        oo = new MemoryStream(buff);
                        Login_a_Parameters login_a = Serializer.Deserialize<Login_a_Parameters>(oo);
                        m_User_info = login_a.user_info;
                        Debug.Log("Login user id:" + m_User_info.user_id);

                    //    StartCoroutine(DailySignIn());
                    //    StartCoroutine(Revive(888,1));
                    //    StartCoroutine(EndLevel(888));
                    //    StartCoroutine(EnterLevel(1));
                    //    StartCoroutine(UpdateEquip());
                    //    StartCoroutine(mail((uint)Mail_Type.mail_friend_req + (uint)Mail_Type.mail_comm));
                    //    StartCoroutine(GetAssist());
                    //    StartCoroutine(GetUserInfo(65545));
                        int daily_task_finished_count = m_User_info.daily_task.daily_task_finished.Count;
                        Debug.Log("DailyTaskFinished Count" + daily_task_finished_count);
                        for (int d = 0; d < daily_task_finished_count; ++d)
                        {
                            Debug.Log("Finished id:" + m_User_info.daily_task.daily_task_finished[d]);
                        }
                        if (daily_task_finished_count == 0)
                        {
                           // StartCoroutine(DailyTaskFinish(1)); 
                        }
                        Debug.Log("coin:" + m_User_info.coins + ",Crystal:" + m_User_info.crystal);
                        //StartCoroutine(BuyGoods(1, 1));
                        //StartCoroutine(UpdateActivityStatus(1, 2));
                    }
                    else
                    {
                        Debug.Log("Login Error: " + rec_c.cmd_result);
                    }
                }
            }
        }
    }

    IEnumerator DailySignIn()
    {
        A8_CMD a8 = new A8_CMD();
        a8.user_id = m_User_info.user_id;
        a8.key = System.Text.Encoding.Default.GetBytes(SystemInfo.deviceUniqueIdentifier);

        CMD cmd = new CMD();
        cmd.cmd_id = (uint)Command_ID.cmd_daily_sign_r;

        a8.cmd_list.Add(cmd);

        Stream streamA8 = new MemoryStream();
        Serializer.Serialize<A8_CMD>(streamA8, a8);

        streamA8.Position = 0;
        int length = (int)streamA8.Length;
        byte[] buffer = new byte[length];
        streamA8.Read(buffer, 0, length);
        WWW getData = new WWW(szUrl, buffer);
        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            Stream stream_rec = new MemoryStream(getData.bytes);

            A8_CMD rec_cmd = Serializer.Deserialize<A8_CMD>(stream_rec);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_daily_sign_a)
                {
                    if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                    {
                        System.IO.Stream oo;
                        byte[] buff = rec_c.cmd_parameter;
                        oo = new MemoryStream(buff);
                        Daily_signed_a_Parameters sign_a = Serializer.Deserialize<Daily_signed_a_Parameters>(oo);
                        Debug.Log("Sign: " + sign_a.sign_reward.coins);
                    }
                    else
                    {
                        Debug.Log("Error: " + rec_c.cmd_result);
                    }

                    StartCoroutine(mail(((uint)Mail_Type.mail_comm+(uint)Mail_Type.mail_reward)));
                }
            }
        }
    }

    IEnumerator EnterLevel(uint level_id)
    {
        A8_CMD a8 = new A8_CMD();
        EnterLevel_r_Parameters param = new EnterLevel_r_Parameters();
        param.level_id = level_id;
        byte[] buffer = S_CMD<EnterLevel_r_Parameters>(ref a8, (uint)Command_ID.cmd_enterlevel_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_enterlevel_a)
                    {
                        EnterLevel_a_Parameters a = DT<EnterLevel_a_Parameters>(rec_c.cmd_parameter);
                        Debug.Log("Enter Level: " + a.level_id);
                    //    StartCoroutine(Revive(a.level_id, 1));
                        StartCoroutine(EndLevel((int)a.level_id));
                    }
                }
                else
                {
                    Debug.Log("EnterLevel Error: " + rec_c.cmd_result);
                }
            }
        }
    }

    IEnumerator EndLevel(int level_id) 
    {
        A8_CMD a8 = new A8_CMD();
        EndLevel_r_Parameters param = new EndLevel_r_Parameters();
        param.level_score = new LevelScore();
        param.level_score.level_id = level_id;
        param.level_score.level_score = 4000;// Random.Range(100, 1000);
        param.level_score.chapter_count = (uint)Random.Range(2, 10);
        byte[] buffer = S_CMD<EndLevel_r_Parameters>(ref a8, (uint)Command_ID.cmd_endlevel_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_endlevel_a)
                    {
                        EndLevel_a_Parameters a = DT<EndLevel_a_Parameters>(rec_c.cmd_parameter);
                        Debug.Log("End Level: " + a.level_score.level_id
                            + "Score:" + a.level_score.level_score
                            + "Chapter:" + a.level_score.chapter_count);
                        Debug.Log("End Rank:" + a.level_rank.rank_index);
                        StartCoroutine(GetRank((uint)a.level_score.level_id, 1, 100, 0));
                    }
                }
                else
                {
                    Debug.Log("EnterLevel Error: " + rec_c.cmd_result);
                }
            }
        }
    }

    IEnumerator Revive(uint level_id, uint r_c)
    {
        A8_CMD a8 = new A8_CMD();
        Arena_Revive_r_Parameters param = new Arena_Revive_r_Parameters();
        param.level_id = level_id;
        param.revive_count = 1;
        byte[] buffer = S_CMD<Arena_Revive_r_Parameters>(ref a8, (uint)Command_ID.cmd_revive_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_revive_a)
                {
                    Arena_Revive_a_Parameters a = DT<Arena_Revive_a_Parameters>(rec_c.cmd_parameter);
                    Debug.Log("Revive: " + a.revive_count);
                    StartCoroutine(EndLevel(888));
                }
            }
        }
    }

    IEnumerator GetRank(uint level_id, int f, int t, uint fr)
    {
        A8_CMD a8 = new A8_CMD();
        Rank_r_Parameters param = new Rank_r_Parameters();
        param.level_id = level_id;
        param.rank_from = 0;
        param.rank_to = 100;
        param.friend_rank = fr;
        byte[] buffer = S_CMD<Rank_r_Parameters>(ref a8, (uint)Command_ID.cmd_rank_req, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_rank_ack)
                {
                    Rank_a_Parameters a = DT<Rank_a_Parameters>(rec_c.cmd_parameter);
                    int r_s = a.rank_list.Count;
                    Debug.Log("GetRank: " + a.level_id + "Count:" + r_s);
                    for (int j = 0; j < r_s; ++j)
                    {
                        Debug.Log(a.rank_list[j].user_id + ";" + a.rank_list[j].level_score);
                    }

                    StartCoroutine(GetFriendRecommand(100));
                }
            }
        }
    }

    IEnumerator UpdateEquip()
    {
        A8_CMD a8 = new A8_CMD();
        Update_Equip_r_Parameter param = new Update_Equip_r_Parameter();
        Item item = new Item();
        item.item_id = 111;
        item.item_count = 2222;
        param.equit_list.Add(item);
        byte[] buffer = S_CMD<Update_Equip_r_Parameter>(ref a8, (uint)Command_ID.cmd_equip_update_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_equip_update_a)
                {
                    Update_Equip_a_Parameter a = DT<Update_Equip_a_Parameter>(rec_c.cmd_parameter);
                    Debug.Log("UpdateEquip: " + a.equit_list.Count);
                    StartCoroutine(GetUserInfo(rec_cmd.user_id));
                }
            }
        }
    }

    IEnumerator GetUserInfo(uint req_id)
    {
        A8_CMD a8 = new A8_CMD();
        UserInfo_r_Parameters param = new UserInfo_r_Parameters();
        param.req_id = req_id;
        byte[] buffer = S_CMD<UserInfo_r_Parameters>(ref a8, (uint)Command_ID.cmd_userinfo_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_id == (uint)Command_ID.cmd_userinfo_a)
                {
                    UserInfo_a_Parameters a = DT<UserInfo_a_Parameters>(rec_c.cmd_parameter);
                    Debug.Log("GetUserInfo: " + a.req_info.user_equip.Count);
                    for (int k = 0; k < a.req_info.user_equip.Count; ++k)
                    {
                        Debug.Log("Equip: " + k + " = " + a.req_info.user_equip[k].item_id);
                    }
                //    StartCoroutine(GetRank(888, 1, 20, 0));
                }
            }
        }
    }

    IEnumerator GetFriendRecommand(uint r_count)
    {
        A8_CMD a8 = new A8_CMD();
        Friend_Recommand_r_Parameters param = new Friend_Recommand_r_Parameters();
        param.req_count = r_count;
        byte[] buffer = S_CMD<Friend_Recommand_r_Parameters>(ref a8, (uint)Command_ID.cmd_friend_recommand_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_friend_recommand_a)
                    {
                        Friend_Recommand_a_Parameters a = DT<Friend_Recommand_a_Parameters>(rec_c.cmd_parameter);
                        Debug.Log("GetFriendRecommand: " + a.recommand_list.Count);
                        // StartCoroutine(EndLevel((int)a.level_id));
                    }
                }
                else
                {
                    Debug.Log("GetFriendRecommand Error: " + rec_c.cmd_result);
                }
            }
        }
    }

    IEnumerator GetAssist()
    {
        A8_CMD a8 = new A8_CMD();
        byte[] buffer = S_CMD(ref a8, (uint)Command_ID.cmd_player_assist_list_r);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_player_assist_list_a)
                    {
                        Player_Assist_List_a_Parameter a = DT<Player_Assist_List_a_Parameter>(rec_c.cmd_parameter);
                        Debug.Log("GetAssist: " + a.assist_list.Count);
                        for (int k = 0; k < a.assist_list.Count; ++k)
                        {
                            Debug.Log("Assist:" + a.assist_list[k].user_id + "EQ Count:" + a.assist_list[k].user_equip.Count);
                            for (int m = 0; m < a.assist_list[k].user_equip.Count; ++m)
                            {
                                Debug.Log("EQ " + m + " : " + a.assist_list[k].user_equip[m].item_id);
                            }
                        }
                    }
                }
                else
                {
                    Debug.Log("GetAssist Error: " + rec_c.cmd_result);
                }
            }
        }
    }

    IEnumerator DailyTaskFinish(uint t_id)
    {
        A8_CMD a8 = new A8_CMD();
        Daily_Task_r_Parameters param = new Daily_Task_r_Parameters();
        param.task_id = t_id;
        byte[] buffer = S_CMD<Daily_Task_r_Parameters>(ref a8, (uint)Command_ID.cmd_daily_task_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_daily_task_a)
                    {
                        Daily_Task_a_Parameters a = DT<Daily_Task_a_Parameters>(rec_c.cmd_parameter);
                        Debug.Log("DailyTaskFinis: " + a.task_id);
                    }
                }
                else
                {
                    Debug.Log("DailyTaskFinis Error: " + rec_c.cmd_result);
                }
            }
        }
    }

    IEnumerator AddFriend(uint f_id)
    {
        A8_CMD a8 = new A8_CMD();
        Friend_Invite_r_Parameters param = new Friend_Invite_r_Parameters();
        param.friend_id = f_id;
        byte[] buffer = S_CMD<Friend_Invite_r_Parameters>(ref a8, (uint)Command_ID.cmd_friend_invite_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_friend_invite_a)
                    {
                        Friend_Invite_a_Parameters a = DT<Friend_Invite_a_Parameters>(rec_c.cmd_parameter);
                        Debug.Log("AddFriend: " + a.friend_id);
                    }
                }
                else
                {
                    Debug.Log("AddFriend Error: " + rec_c.cmd_result);
                }
            }
        }
    }

    IEnumerator BuyGoods(uint goods_id, uint goods_count)
    {
        A8_CMD a8 = new A8_CMD();
        Buy_Goods_r_Parameter param = new Buy_Goods_r_Parameter();
        param.goods_id = goods_id;
        param.goods_count = goods_count;
        byte[] buffer = S_CMD<Buy_Goods_r_Parameter>(ref a8, (uint)Command_ID.cmd_buy_goods_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            A8_CMD rec_cmd = DT<A8_CMD>(getData.bytes);

            int cmd_size = rec_cmd.cmd_list.Count;
            for (int i = 0; i < cmd_size; ++i)
            {
                CMD rec_c = rec_cmd.cmd_list[i];
                if (rec_c.cmd_result == (uint)Command_Result.cmd_no_error)
                {
                    if (rec_c.cmd_id == (uint)Command_ID.cmd_buy_goods_a)
                    {
                        Buy_Goods_a_Parameter a = DT<Buy_Goods_a_Parameter>(rec_c.cmd_parameter);
                        Debug.Log("BuyGoods: " + a.goods_id + ", Count:" + a.goods_count);
                        Debug.Log("BuyGoods Cost, Coin:" + a.goods_cost.coins + ",Crystals:" + a.goods_cost.crystals
                            +",Scores:" + a.goods_cost.scores + ",RMB:" + a.goods_cost.rmb);
                    }
                }
                else
                {
                    Debug.Log("BuyGoods Error: " + rec_c.cmd_result);
                }
            }
        }
    }
    IEnumerator UpdateActivityStatus(uint activity_id, uint status)
    {
        A8_CMD a8 = new A8_CMD();
        UpdateActivity_Status_r_Parameter param = new UpdateActivity_Status_r_Parameter();
        param.activity_id = activity_id;
        param.activity_status = status;
        byte[] buffer = S_CMD<UpdateActivity_Status_r_Parameter>(ref a8, (uint)Command_ID.cmd_activity_interactive_r, ref param);
        WWW getData = new WWW(szUrl, buffer);

        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            UpdateActivity_Status_a_Parameter a = new UpdateActivity_Status_a_Parameter();
            uint cmd_result = CheckResult<UpdateActivity_Status_a_Parameter>(getData.bytes, (uint)Command_ID.cmd_activity_interactive_a, ref a);
            if (cmd_result == (uint)Command_Result.cmd_no_error)
            {
                Debug.Log("a id:" + a.activity_id + ",status:" + a.activity_status);
                Debug.Log("activity reward: Coin" + a.activity_reward.coins + ",Crystal" +
                    a.activity_reward.crystals);
            }
            else
            {
                Debug.Log("UpdateActivityStatus Error: " + cmd_result);
            }
        }
    }
    // Update is called once per frame
	void Update () {
	
	}
    
    private uint get_liner_hash1(string i_key, uint initVal)
    {
        System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
        byte[] k = ue.GetBytes(i_key);
        uint a, b, c;  // the internal state
        uint len;               // how many key bytes still need mixing

        // Set up the internal state 
        len = (uint)k.Length;
        uint i_len = len;
        a = b = 0x9e3779b9; // the golden ratio; an arbitrary value
        c = initVal;              // always initialize this to 0

        //---------------------------------------- handle most of the key
        while (len >= 12)
        {
            a = a + (k[0] + ((uint)k[1] << 8) + ((uint)k[2] << 16) +
                ((uint)k[3] << 24));
            b = b + (k[4] + ((uint)k[5] << 8) + ((uint)k[6] << 16) +
                ((uint)k[7] << 24));
            c = c + (k[8] + ((uint)k[9] << 8) + ((uint)k[10] << 16) +
                ((uint)k[11] << 24));
            MIX(ref a, ref b, ref c);
            len = len - 12;
            byte[] t = k;
            System.Array.Copy(t, 12, k, 0, len);
        }

        //------------------------------------- handle the last 11 bytes
        c = c + i_len;
        Switchs(len, ref a, ref b, ref c, k);
        MIX(ref a, ref b, ref c);

        //-------------------------------------------- report the result
        return c;
    }

    private void Switchs(uint len, ref uint a, ref uint b, ref uint c, byte[] k)
    {
        switch (len)
        {
            case 11:
                c = c + ((uint)k[10] << 24);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 10:
                c = c + ((uint)k[9] << 16);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 9:
                c = c + ((uint)k[8] << 8);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;

            // the first byte of c is reserved for the length
            case 8:
                b = b + ((uint)k[7] << 24);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 7:
                b = b + ((uint)k[6] << 16);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 6:
                b = b + ((uint)k[5] << 8);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 5:
                b = b + k[4];
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;

            case 4:
                a = a + ((uint)k[3] << 24);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 3:
                a = a + ((uint)k[2] << 16);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 2:
                a = a + ((uint)k[1] << 8);
                Switchs(len - 1, ref a, ref b, ref c, k);
                break;
            case 1:
                a = a + k[0];
                break;
        }
    }
    
    private uint get_liner_hash(string i_key, uint initVal)
    {
        char[] k = i_key.ToCharArray();
       /* fixed (char* k = &k1[0])
        {

        }*/
        uint a, b, c;  // the internal state
        uint len;               // how many key bytes still need mixing

        // Set up the internal state 
        len = (uint)k.Length;
        uint i_len = len;
        a = b = 0x9e3779b9; // the golden ratio; an arbitrary value
        c = initVal;              // always initialize this to 0

        //---------------------------------------- handle most of the key
        while (len >= 12)
        {
            a = a + (k[0] + ((uint)k[1] << 8) + ((uint)k[2] << 16) +
                ((uint)k[3] << 24));
            b = b + (k[4] + ((uint)k[5] << 8) + ((uint)k[6] << 16) +
                ((uint)k[7] << 24));
            c = c + (k[8] + ((uint)k[9] << 8) + ((uint)k[10] << 16) +
                ((uint)k[11] << 24));
            MIX(ref a, ref b, ref c);
            //k = k + 12;
            k = i_key.Substring(12).ToCharArray();

            len = len - 12;
        }

        //------------------------------------- handle the last 11 bytes
        c = c + i_len;
        Switchs(len, ref a, ref b, ref c, k);
        MIX(ref a, ref b, ref c);

        //-------------------------------------------- report the result
        return c;
    }

    private void Switchs(uint len, ref uint a, ref uint b, ref uint c, char[] k)
    {
        if (len > 10)
        {            c = c + ((uint)k[10] << 24);}
        if(len > 9)
        {        c = c + ((uint)k[9] << 16);}
        if(len > 8)
        {        c = c + ((uint)k[8] << 8);}
        if(len > 7)
        {        b = b + ((uint)k[7] << 24);}
         if(len >  6)
         {       b = b + ((uint)k[6] << 16);}  
        if(len > 5)
        {       b = b + ((uint)k[5] << 8);}
         if(len > 4)
         {      b = b + k[4];}
         if(len > 3)
         {       a = a + ((uint)k[3] << 24);}
        if(len > 2)
        {       a = a + ((uint)k[2] << 16);}
        if(len > 1)
        {        a = a + ((uint)k[1] << 8);}
        if(len > 0)
        {      a = a + k[0];}
    }
    private void MIX(ref uint a, ref uint b, ref uint c)
    {
        a = a - b; a = a - c; a = a ^ (c >> 13);
        b = b - c; b = b - a; b = b ^ (a << 8);
        c = c - a; c = c - b; c = c ^ (b >> 13);
        a = a - b; a = a - c; a = a ^ (c >> 12);
        b = b - c; b = b - a; b = b ^ (a << 16);
        c = c - a; c = c - b; c = c ^ (b >> 5);
        a = a - b; a = a - c; a = a ^ (c >> 3);
        b = b - c; b = b - a; b = b ^ (a << 10);
        c = c - a; c = c - b; c = c ^ (b >> 15);
    }
}
 